﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ChatServer
{
    public class Global : HttpApplication
    {
        private static List<Message> _messages;

        /// <summary>
        /// List of the stacked messages
        /// </summary>
        public static List<Message> Messages
        {
            get
            {
                ChatServices chatServices = new ChatServices();
                List<Message> messagesTimedOut = _messages.Where(message => DateTime.Now.Subtract(message.Send).Minutes >= 5).ToList();
                foreach (Message message in messagesTimedOut)
                    if (message.From != message.To && chatServices.GetUserByUserID(message.From) != null)
                        message.ConvertToFailMessage();
                    else
                        _messages.Remove(message);
                return _messages;
            }
            private set { _messages = value; }
        }

        /// <summary>
        /// List of all users
        /// </summary>
        static public List<User> Users { get; private set; }

        protected void Application_Start(object sender, EventArgs e)
        {
            Users = new List<User>();
            Messages = new List<Message>();
        }
        protected void Session_Start(object sender, EventArgs e) { }
        protected void Application_BeginRequest(object sender, EventArgs e) { }
        protected void Application_AuthenticateRequest(object sender, EventArgs e) { }
        protected void Application_Error(object sender, EventArgs e) { }
        protected void Session_End(object sender, EventArgs e) { }
        protected void Application_End(object sender, EventArgs e) { }
    }
}